//
//  CTFile.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/24.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTFile.h"
#import "CTDebugManager.h"

@implementation CTFile


//归档数据

+(void)cacheFile:(NSString *)file
        fileName:(NSString *)name
         saveDoc:(CTFileDoc)docType
        savePath:(NSString *)path
{
    @try {
        
        NSString * docName;
        switch (docType) {
            case CTFileDocDocuments:
                docName = @"Documents";
                break;
            case CTFileDocLibrary:
                docName = @"Library";
                break;
            case CTFileDocTemporary:
                docName = @"tmp";
                break;
                
            default:
                break;
        }
        
        
        NSString * conf  = [self getCommonCacheAdress:docName withPath:path];
        NSString * filePath = [conf stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",name]];
        NSError * error;
        
        NSFileManager *fileManager=[NSFileManager defaultManager];
        
        if (![fileManager fileExistsAtPath:conf])
        {
            [[NSFileManager defaultManager] createDirectoryAtPath:conf withIntermediateDirectories:YES attributes:nil error:&error];
        }
        
        if (![file writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
            [CTDebugManager showErrorLogInfo:@"缓存数据失败！！"];
        }
       
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"缓存数据失败！！"];
    }
}
+(void)cacheDicFile:(NSMutableDictionary *)file
           fileName:(NSString *)name
            saveDoc:(CTFileDoc)docType
           savePath:(NSString *)path
{
    @try {
        
        NSString * docName;
        switch (docType) {
            case CTFileDocDocuments:
                docName = @"Documents";
                break;
            case CTFileDocLibrary:
                docName = @"Library";
                break;
            case CTFileDocTemporary:
                docName = @"tmp";
                break;
                
            default:
                break;
        }
        
        
        NSString * conf  = [self getCommonCacheAdress:docName withPath:path];
        NSString * filePath = [conf stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",name]];
        NSError * error;
        
        NSFileManager *fileManager=[NSFileManager defaultManager];
        
        if (![fileManager fileExistsAtPath:conf])
        {
            [[NSFileManager defaultManager] createDirectoryAtPath:conf withIntermediateDirectories:YES attributes:nil error:&error];
        }
        
        if (![file writeToFile:filePath atomically:YES]) {
            [CTDebugManager showErrorLogInfo:@"缓存数据失败！！"];
        }
        
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"缓存数据失败！！"];
    }
}
+(void)cacheArrFile:(NSMutableArray *)file
           fileName:(NSString *)name
            saveDoc:(CTFileDoc)docType
           savePath:(NSString *)path
{
    @try {
        
        NSString * docName;
        switch (docType) {
            case CTFileDocDocuments:
                docName = @"Documents";
                break;
            case CTFileDocLibrary:
                docName = @"Library";
                break;
            case CTFileDocTemporary:
                docName = @"tmp";
                break;
                
            default:
                break;
        }
        
        
        NSString * conf  = [self getCommonCacheAdress:docName withPath:path];
        NSString * filePath = [conf stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",name]];
        NSError * error;
        
        NSFileManager *fileManager=[NSFileManager defaultManager];
        
        if (![fileManager fileExistsAtPath:conf])
        {
            [[NSFileManager defaultManager] createDirectoryAtPath:conf withIntermediateDirectories:YES attributes:nil error:&error];
        }
        
        if (![file writeToFile:filePath atomically:YES]) {
            [CTDebugManager showErrorLogInfo:@"缓存数据失败！！"];
        }
        
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"缓存数据失败！！"];
    }
}




//读取数据

+(NSString *)getFile:(NSString *)name
       fromDoc:(CTFileDoc)docType
      fromPath:(NSString *)path
{
    @try {
        
        NSString * docName;
        switch (docType) {
            case CTFileDocDocuments:
                docName = @"Documents";
                break;
            case CTFileDocLibrary:
                docName = @"Library";
                break;
            case CTFileDocTemporary:
                docName = @"tmp";
                break;
                
            default:
                break;
        }
        NSError * error;
        
        NSString * conf  = [self getCommonCacheAdress:docName withPath:path];
        NSString * filePath = [conf stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",name]];
        
        NSString * fileStr = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
        
        return fileStr;
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"获取数据失败！！"];
        return nil;
    }
}
+(NSMutableDictionary *)getDicFile:(NSString *)name
                           fromDoc:(CTFileDoc)docType
                          fromPath:(NSString *)path
{
    @try {
        
        NSString * docName;
        switch (docType) {
            case CTFileDocDocuments:
                docName = @"Documents";
                break;
            case CTFileDocLibrary:
                docName = @"Library";
                break;
            case CTFileDocTemporary:
                docName = @"tmp";
                break;
                
            default:
                break;
        }
        NSString * conf  = [self getCommonCacheAdress:docName withPath:path];
        NSString * filePath = [conf stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",name]];
        NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
        
        return dic;
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"获取数据失败！！"];
        return nil;
    }
}
+(NSMutableArray *)getArrFile:(NSString *)name
                      fromDoc:(CTFileDoc)docType
                     fromPath:(NSString *)path
{
    @try {
        
        NSString * docName;
        switch (docType) {
            case CTFileDocDocuments:
                docName = @"Documents";
                break;
            case CTFileDocLibrary:
                docName = @"Library";
                break;
            case CTFileDocTemporary:
                docName = @"tmp";
                break;
                
            default:
                break;
        }
        
        NSString * conf  = [self getCommonCacheAdress:docName withPath:path];
        NSString * filePath= [conf stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",name]];
        NSMutableArray * arr = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
        
        return arr;
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"获取数据失败！！"];
        return nil;
    }
}



//更新数据


//删除数据
+(void)removeFile:(NSString *)name
          fromDoc:(CTFileDoc)docType
         fromPath:(NSString *)path
{
    @try {
        
        NSString * docName;
        switch (docType) {
            case CTFileDocDocuments:
                docName = @"Documents";
                break;
            case CTFileDocLibrary:
                docName = @"Library";
                break;
            case CTFileDocTemporary:
                docName = @"tmp";
                break;
                
            default:
                break;
        }
        
        NSString * conf  = [self getCommonCacheAdress:docName withPath:path];
        NSString * filePath = [conf stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",name]];
        NSFileManager *fileManager=[NSFileManager defaultManager];
        [fileManager removeItemAtPath:filePath error:nil];
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"删除异常，失败！！"];
    }
}



//文件是否存在
+(BOOL)isExistFile:(NSString *)name
               doc:(CTFileDoc)docType
              path:(NSString *)path
{
    NSString * docName;
    switch (docType) {
        case CTFileDocDocuments:
            docName = @"Documents";
            break;
        case CTFileDocLibrary:
            docName = @"Library";
            break;
        case CTFileDocTemporary:
            docName = @"tmp";
            break;
            
        default:
            break;
    }
    
    NSString * conf  = [self getCommonCacheAdress:docName withPath:path];
    NSString * filePath = [conf stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",name]];
    
    NSFileManager *fileManager=[NSFileManager defaultManager];
    return [fileManager fileExistsAtPath:filePath];
}







+(NSString *)getCommonCacheAdress:(NSString *)doc withPath:(NSString *)path
{
    NSString * documentD = [NSHomeDirectory() stringByAppendingPathComponent:doc];
    NSString * configFile = [documentD stringByAppendingPathComponent:path];
   
    return configFile;
}



@end
